package com.atguigu;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;

import java.io.IOException;

/**
 * ClassName: HbaseTest_01
 * Package: com.atguigu
 * Description:
 *
 * @Author ChenJun
 * @Create 2023/4/22 19:51
 * @Version 1.0
 */
public class HbaseTest_01 {

    private static  Connection connection ;
    // 设置静态属性hbase连接
    static {
        Configuration configuration = HBaseConfiguration.create();
        try {
            connection = ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    //TODO 插入数据
    public static void putData(String tableName, String rowKey, String cf,String column,String value) throws IOException {

        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        //创建put对象
        Put put = new Put(rowKey.getBytes());

        //添加具体的列信息
        put.addColumn(cf.getBytes(),column.getBytes(),value.getBytes());

        //执行put操作
        table.put(put);

        //关闭table对象
        table.close() ;
    }

    public static void main(String[] args) throws IOException {
        //TODO 插入数据
        putData("stu1","1001","info1","name","chenjun");

        //关闭连接
        connection.close();
    }

}
